package br.org.etec.dotcao.dao.jpa;

import javax.persistence.TypedQuery;

import br.org.etec.dotcao.dao.UserDao;
import br.org.etec.dotcao.model.OauthInfo;
import br.org.etec.dotcao.model.User;

public class UserDaoImpl extends JPAImpl<User> implements UserDao {

	public UserDaoImpl() {
		super(User.class);
	}

	@Override
	public User authenticate(String username, String password) {
		TypedQuery<User> query = em.createNamedQuery("selectAuth", User.class);
		query.setParameter("username", username);
		query.setParameter("password", password);
		
		return query.getSingleResult();
	}

	@Override
	public User authenticate(OauthInfo oauth) {
		TypedQuery<User> query = em.createNamedQuery("selectOauth", User.class);
		query.setParameter("oauth", oauth.getCode());
		
		return query.getSingleResult();
	}

	@Override
	public User getByEmail(String email) {
		TypedQuery<User> query = em.createNamedQuery("selectEmail", User.class);
		query.setParameter("username", email);
		
		return query.getSingleResult();
	}
}
